package tercerDia;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.Label;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.Component;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class InstalacionElectrica extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private AparatoElectrico plancha = new AparatoElectrico(1200, false);
	private AparatoElectrico bombilla = new AparatoElectrico(100, false);
	private AparatoElectrico radiador = new AparatoElectrico(2000, false);
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_3;
	private JLabel lblNewLabel_4;
	private JButton btnOn;
	private JButton btnOn_1;
	private JButton btnNewButton;
	private JTextArea textArea;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					InstalacionElectrica frame = new InstalacionElectrica();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	private void pintar(){
		if(plancha.isEncendido()){
			lblNewLabel_2.setIcon(new ImageIcon(InstalacionElectrica.class.getResource("/imagenes/green.png")));
			btnNewButton.setText("OFF");
		}else{
			lblNewLabel_2.setIcon(new ImageIcon(InstalacionElectrica.class.getResource("/imagenes/red.png")));
			btnNewButton.setText("ON");
		}
		if(bombilla.isEncendido()){
			lblNewLabel_3.setIcon(new ImageIcon(InstalacionElectrica.class.getResource("/imagenes/green.png")));
			btnOn.setText("OFF");
		}else{
			lblNewLabel_3.setIcon(new ImageIcon(InstalacionElectrica.class.getResource("/imagenes/red.png")));
			btnOn.setText("ON");
		}
		if(radiador.isEncendido()){
			lblNewLabel_4.setIcon(new ImageIcon(InstalacionElectrica.class.getResource("/imagenes/green.png")));
			btnOn_1.setText("OFF");
		}else{
			lblNewLabel_4.setIcon(new ImageIcon(InstalacionElectrica.class.getResource("/imagenes/red.png")));
			btnOn_1.setText("ON");
		}
		textField.setText(AparatoElectrico.consumo()+"");
		pintarText();
	}

	/**
	 * Create the frame.
	 */
	public InstalacionElectrica() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 654, 441);
		contentPane = new JPanel();
		contentPane.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel_1 = new JLabel("New label");
		lblNewLabel_1.setIcon(new ImageIcon(InstalacionElectrica.class.getResource("/imagenes/plan.png")));
		lblNewLabel_1.setBounds(10, 11, 129, 116);
		contentPane.add(lblNewLabel_1);
		
		JLabel label = new JLabel("New label");
		label.setIcon(new ImageIcon(InstalacionElectrica.class.getResource("/imagenes/bombilla.png")));
		label.setBounds(10, 138, 129, 112);
		contentPane.add(label);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon(InstalacionElectrica.class.getResource("/imagenes/radiador.png")));
		lblNewLabel.setBounds(10, 261, 129, 130);
		contentPane.add(lblNewLabel);
		
		lblNewLabel_2 = new JLabel("New label");
		lblNewLabel_2.setIcon(new ImageIcon(InstalacionElectrica.class.getResource("/imagenes/red.png")));
		lblNewLabel_2.setBounds(177, 37, 71, 65);
		contentPane.add(lblNewLabel_2);
		
		lblNewLabel_3 = new JLabel("New label");
		lblNewLabel_3.setIcon(new ImageIcon(InstalacionElectrica.class.getResource("/imagenes/red.png")));
		lblNewLabel_3.setBounds(177, 162, 71, 65);
		contentPane.add(lblNewLabel_3);
		
		lblNewLabel_4 = new JLabel("New label");
		lblNewLabel_4.setIcon(new ImageIcon(InstalacionElectrica.class.getResource("/imagenes/red.png")));
		lblNewLabel_4.setBounds(177, 294, 71, 65);
		contentPane.add(lblNewLabel_4);
		
		btnNewButton = new JButton("ON");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(btnNewButton.getText().equals("ON"))
					plancha.enciende();
				else
					plancha.apaga();
				pintar();
			}
		});
		btnNewButton.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
		btnNewButton.setBounds(288, 37, 108, 65);
		contentPane.add(btnNewButton);
		
		btnOn = new JButton("ON");
		btnOn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(btnOn.getText().equals("ON"))
					bombilla.enciende();
				else
					bombilla.apaga();
				pintar();
			}
		});
		btnOn.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
		btnOn.setBounds(288, 162, 108, 65);
		contentPane.add(btnOn);
		
		btnOn_1 = new JButton("ON");
		btnOn_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(btnOn_1.getText().equals("ON"))
					radiador.enciende();
				else
					radiador.apaga();
				pintar();
			}
		});
		btnOn_1.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
		btnOn_1.setBounds(288, 294, 108, 65);
		contentPane.add(btnOn_1);
		
		JLabel lblConsumoTotal = new JLabel("CONSUMO TOTAL:");
		lblConsumoTotal.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
		lblConsumoTotal.setBounds(436, 37, 192, 26);
		contentPane.add(lblConsumoTotal);
		
		textField = new JTextField();
		textField.setAlignmentX(Component.RIGHT_ALIGNMENT);
		textField.setHorizontalAlignment(JTextField.RIGHT);
		textField.setText(""+0);
		textField.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
		textField.setBounds(436, 82, 160, 26);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblW = new JLabel("w");
		lblW.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
		lblW.setBounds(606, 82, 46, 23);
		contentPane.add(lblW);
		
		textArea = new JTextArea();
		textArea.setFont(new Font("Trebuchet MS", Font.BOLD, 16));
		textArea.setBounds(436, 181, 192, 139);
		contentPane.add(textArea);
	}
	private void pintarText(){
		textArea.setText("Plancha: "+plancha+"\nBombilla: "+bombilla+"\nRadiador: "+radiador);

	}
}
